package br.com.folha.controller;

import br.com.folha.dao.UsuarioDao;
import br.com.folha.entity.Usuario;
import java.io.IOException;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
 
@ManagedBean
@SessionScoped
public class LoginController implements Serializable{
 
    /**
         * 
         */
    private static final long serialVersionUID = -5140496153854040344L;
    public Usuario Usuario;
    private HttpSession session;
    private UsuarioDao dao;
    
    private boolean usuAdm;
    private boolean usuNotAdm;
    private boolean usuDir;
    private boolean usuCoord;
    private boolean usuFin;
 
    public LoginController() {        
        
        Usuario = new Usuario();
        dao     = new UsuarioDao();
        SecurityContext context = SecurityContextHolder.getContext();
        if (context instanceof SecurityContext){
            Authentication authentication = context.getAuthentication();
            
            if (authentication instanceof Authentication){                  
                Usuario.setLogin(((User)authentication.getPrincipal()).getUsername());                  
                setUsuario(dao.consultaUsuario(Usuario.getLogin()));                
            }
        }
    }    
    
        public Usuario getUsuario() {            
                return Usuario;
        }

        public void setUsuario(Usuario usuario) {
                Usuario = usuario;
        }
            
           
    public void logout() throws IOException{
        getRequest().getSession().invalidate();        
        FacesContext.getCurrentInstance().getExternalContext().redirect("../login.jsf");        
    }
    
    private HttpServletRequest getRequest() {
        return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    }

    public boolean isUsuAdm() {        
        if(Usuario.getGrupo() == 2){
            return true;
        }else{
            return false;
        }
                
    }
    
    public boolean isUsuDir() {
        
        if(Usuario.getGrupo() == 4){
            
            return true;
        }else{
            
            return false;
        }
        
    }    

    public void setUsuAdm(boolean usuAdm) {
        this.usuAdm = usuAdm;
    }

    public boolean isUsuNotAdm() {
        
        if(Usuario.getGrupo() != 2){
            return true;
        }else{
            return false;
        }
        
    }

    public void setUsuNotAdm(boolean usuNotAdm) {
        this.usuNotAdm = usuNotAdm;
    }

    public void setUsuDir(boolean usuDir) {
        this.usuDir = usuDir;
    }

    public boolean isUsuCoord() {
        
        if(Usuario.getGrupo() != 5){
            return true;
        }else{
            return false;
        }        
                
    }

    public void setUsuCoord(boolean usuCoord) {
        this.usuCoord = usuCoord;
    }

    public boolean isUsuFin() {
        
        if(Usuario.getGrupo() != 3 && Usuario.getGrupo() != 6){
            return true;
        }else{
            return false;
        }         
        
    }

    public void setUsuFin(boolean usuFin) {
        this.usuFin = usuFin;
    }
     
}